﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using umbraco.cms.businesslogic.utilities;

using tswe.umbraco.languageEditor.classes;
using tswe.umbraco.languageEditor.classes.xml;

namespace tswe.umbraco.languageEditor
{
    public partial class frmMain : Form
    {
        private string langTwoFileName = null;
        private string langOneFileName = null;
        
        private Language langOne;
        private Language langTwo;
        private Language langBase; // most common the englisch language version en.xml

        public frmMain()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "Open reference language file";
            openFileDialog1.FileName = "en.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                langBase = Language.Load(openFileDialog1.FileName);
                fillBaseTree(langBase);

                button1.Enabled = true;
                button2.Enabled = true;
            }
        }

        private void fillBaseTree(Language language)
        {
            treeView1.Nodes.Clear();

            foreach (Area area in language.Areas)
            {
                TreeNode tn = treeView1.Nodes.Add(area.Alias);
                addKeyNodes(tn, area);
            }
        }

        private void addKeyNodes(TreeNode treeNode, Area area)
        {
            foreach (Key key in area.Keys)
            {
                TreeNode tn = treeNode.Nodes.Add(key.Alias);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                saveLanguageValues();
                clearDisplays();
                return;
            }

            string key = e.Node.Text;
            string area = e.Node.Parent.Text;
            updateLanguageValues(area, key);
        }

        private void clearDisplays()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();

            // clear languages last key remember function
            if (langBase != null)
                langBase.Text(null, null);
            if (langOne != null)
                langOne.Text(null, null);
            if (langTwo != null)
                langTwo.Text(null, null);
        }

        private void updateLanguageValues(string area, string key)
        {
            if (langBase != null)
            {
                langBase.UpdateLastKey(textBox3.Text);
                textBox3.Text = langBase.Text(area, key);
            }
            if (langOne != null)
            {
                langOne.UpdateLastKey(textBox1.Text);
                textBox1.Text = langOne.Text(area, key);
            }
            if (langTwo != null)
            {
                langTwo.UpdateLastKey(textBox2.Text);
                textBox2.Text = langTwo.Text(area, key);
            }
        }

        private void saveLanguageValues()
        {
            if (langBase != null)
            {
                langBase.UpdateLastKey(textBox3.Text);
            }
            if (langOne != null)
            {
                langOne.UpdateLastKey(textBox1.Text);
            }
            if (langTwo != null)
            {
                langTwo.UpdateLastKey(textBox2.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string title = "Open a language file for compare";
            if (loadLanguage(ref langOne, ref langOneFileName, ref title))
                button3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string title = "Save compare language file";
            saveLanguage(langOne, langOneFileName, ".new1.xml", ref title);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string title = "Open a language file to modify";
            if (loadLanguage(ref langTwo, ref langTwoFileName, ref title))
                button4.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string title = "Save modified language file";
            saveLanguage(langTwo, langTwoFileName, ".modified.xml", ref title);
        }

        private bool loadLanguage(ref Language language, ref string fileName, ref string title)
        {
            openFileDialog1.Title = title;

            if (!String.IsNullOrEmpty(fileName))
                openFileDialog1.FileName = fileName;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog1.FileName;
                language = Language.Load(fileName);
                return true;
            }
            return false;
        }

        private void saveLanguage(Language language, string fileName, string extention, ref string title)
        {
            saveLanguageValues();

            saveFileDialog1.Title = title;
            
            if (String.IsNullOrEmpty(fileName)) return;

            fileName = Path.ChangeExtension(fileName, extention);
            saveFileDialog1.InitialDirectory = Path.GetFullPath(fileName);
            saveFileDialog1.FileName = Path.GetFileName(fileName);

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                language.TidyUpBy(langBase);

                language.Save(saveFileDialog1.FileName);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            updateDiffView();
        }

        private void updateDiffView()
        {
            Helpers.Diff2RichTextBox(textBox1.Text, textBox2.Text, ref richTextBox1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button4.Enabled = false;
        }
    }
}
